package net.wsfcp.android.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.wsfcp.android.http.mvp.IView;
import net.wsfcp.android.utils.DpUtils;
import net.wsfcp.android.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

/**
 * Created by Shaoboyang on 2018/12/14.
 */
//基类activity
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements
        IView, View.OnClickListener {
    protected View view;

    protected P mPresenter;


    protected RelativeLayout toolbar;
    protected RelativeLayout btnLeft;
    protected Button btRight;
    protected TextView mTitle;

    private ProgressDialog pg;
    private static Toast toast = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getView());
        mPresenter = loadPresenter();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        pg = new ProgressDialog(this);//等待框
        initCommonData();
        initView();
        initListener();
        initData();
    }


    public void showPg() {
        pg.show();
    }

    public void dismissPg() {
        pg.dismiss();
    }


    public void setLeftClick(View.OnClickListener listener) {
        if (btnLeft != null) {
            btnLeft.setOnClickListener(listener);
        }
    }

    public void setRightClick(View.OnClickListener listener) {
        if (btRight != null) {
            btRight.setOnClickListener(listener);
        }
    }


    public void setmTitle(String str) {
        if (mTitle != null) {
            mTitle.setText(str);
        }
    }


    protected abstract P loadPresenter();

    private void initCommonData() {

        if (mPresenter != null)
            mPresenter.attachView(this);
    }

    protected abstract void initData();

    protected abstract void initListener();

    protected abstract void initView();

    protected abstract int getLayoutId();

    protected abstract void otherViewClick(View view);

    /**
     * @return 显示的内容
     */
    public View getView() {
        view = View.inflate(this, getLayoutId(), null);
        return view;
    }

    /**
     * 点击的事件的统一的处理
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            default:
                otherViewClick(view);
                break;
        }

    }




    /**
     * @param str 显示一个内容为str的toast
     */
    public void toast(String str) {

        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

//        if (toast == null) {
//            toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
//        } else {
//            toast.setText(str);
//            toast.setDuration(Toast.LENGTH_SHORT);
//        }
//        toast.show();

    }

    /**
     * @param contentId 显示一个内容为contentId指定的toast
     */
    public void toast(int contentId) {
        Toast.makeText(this, contentId, Toast.LENGTH_SHORT).show();
    }


    /**
     * @param str 日志的处理
     */
    public void LogE(String str) {
        LogUtils.e(getClass(), str);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();

        //取消eventbus注册
        EventBus.getDefault().unregister(this);
    }


    protected void startActivity(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }


    public int dp(float dpValue) {
        return DpUtils.dp2px(dpValue);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Object event) {
    }
}
